package pack0824;

import java.io.*;

public class WorkA {
    //1.遍历D盘下的文件夹
    public static void a(){
        File file = new File("D://");
        File[] chileFile = file.listFiles();
        for(int i = 0; i<chileFile.length; i++){
            File f = chileFile[i];
            System.out.println(f.getName());
        }
    }
    //2.读取某个指定的文件内容
    public static StringBuffer b(){
        StringBuffer s = new StringBuffer("");
        int len;
        try {
            File file= new File("D://a//a.txt");
            InputStream in = new FileInputStream(file);
            byte[] bytes = new byte[200];
            do {
                len = in.read(bytes);
                if(len!=-1){
                    String str = new String(bytes,0,len);
                    s.append(str);
                }
            }while (len!=-1);
            System.out.print(s);
            in.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return s;
    }
    //3.实现文件复制操作，将D盘下的a.txt复制到G盘下
    public static void c(){
        File file1 = new File("G://a.txt");
        try{
            file1.createNewFile();
        }catch (IOException e){
            System.out.println(e.getMessage());
        }

        b();
        StringBuffer a = b();
        String s = a.toString();
        try{
            OutputStream out = new FileOutputStream(file1);
            byte[] bytes = s.getBytes();
            out.write(bytes);
            out.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    //4.实现文件复制操作，将D盘下的a.jpg复制到G盘下
    public static void d(){
        StringBuffer s = new StringBuffer("");
        int len;
        try {
            File file= new File("D://a//a.jpeg");
            File file1 = new File("G://b.jpeg");//复制到G盘，重新命名（可以一样）
            //建立输入输出通道
            InputStream in = new FileInputStream(file);
            OutputStream out = new FileOutputStream(file1);
            byte[] bytes = new byte[1024];
            do {
                len = in.read(bytes);
                if(len!=-1){
                    //直接输出字符串数组形成新的图片，系统会在G盘自动新建文件，无需手动建立
                    out.write(bytes);
                }
            }while (len!=-1);
            in.close();
            out.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }
    //5.实现文件剪切操作，将D盘下的a.txt剪切到G盘下
    public static void e(){
        c();
        File file = new File("D://a//a.txt");
        file.delete();
    }
    //4.实现文件复制操作，将D盘下的a.jpg复制到G盘下
    public static void f(){
        File file1 = new File("D://a//a");
        File file2 = new File("D://a//b");
    }

    public static void main(String[] args) {
        d();
    }
}
